﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PInitiatives.Models;
using System.Net.Mail;
using System.Net;

namespace PInitiatives.Helpers
{
    public static class Email
    {
        public static void SendEmail(string addressee, string subject, string body)
        {
            string emailAdress = SettingsManager.GetSettingValue("System.Email.Address");
            string passwd = SettingsManager.GetSettingValue("System.Email.Password");
            MailMessage mailMessage = new MailMessage(emailAdress, addressee);
            mailMessage.Subject = subject;
            mailMessage.Body = body;

            SmtpClient smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(emailAdress, passwd)
            };

            smtp.Send(mailMessage);
        }
    }

    public static class ReviewHelper
    {
        public static IQueryable<Review> GetInitativesReviewById(int id)
        {
            PInitiativesEntities db = new PInitiativesEntities();
            return db.Reviews.Where(r => r.InitiativeID == id && r.InitiativeRate != -1);
        }

        public static void RefreshUserRate(int userID)
        {
            try
            {
                PInitiativesEntities db = new PInitiativesEntities();
                User user = db.Users.SingleOrDefault(u => u.ID == userID);
                var reviews = user.UserReviews.OrderBy(r => r.InitiativeID);
                double rate = 0;
                int count = 0;
                double tmpRate = 0;
                int tmpCount = 0;
                int? previousInitative = null;

                foreach (Review review in reviews)
                {
                    if (review.InitiativeRate != -1)
                    {
                        if (previousInitative == review.InitiativeID)
                        {
                            tmpRate += review.AuthorRate;
                            tmpCount++;
                        }
                        else
                        {
                            if (tmpCount != 0)
                            {
                                rate += tmpRate / tmpCount;
                                count++;
                            }
                            tmpRate = review.AuthorRate;
                            tmpCount = 1;
                        }
                    }
                    else
                    {
                        if (tmpCount != 0)
                        {
                            rate += tmpRate / tmpCount;
                            count++;
                            tmpRate = 0;
                            tmpCount = 0;
                        }

                        rate += review.AuthorRate;
                        count++;
                    }
                    previousInitative = review.InitiativeID;
                }

                if (tmpCount != 0)
                {
                    rate += tmpRate / tmpCount;
                    count++;
                }
                if (count != 0)
                    user.Rate = rate / count;
                else
                    user.Rate = rate;

                db.SaveChanges();
            }
            catch { }
        }

        public static void RefreshInitiativeRate(int initiativeID)
        {
            PInitiativesEntities db = new PInitiativesEntities();
            Initiative initiative = db.Initiatives.SingleOrDefault(i => i.ID == initiativeID);

            try
            {
                var reviews = db.Reviews.Where(r => r.InitiativeID == initiativeID && r.InitiativeRate != -1);
                float rate = 0;
                int count = 0;
                foreach (Review review in reviews)
                {
                    rate += review.InitiativeRate;
                    count++;
                }

                if (count != 0)
                    initiative.Rate = rate / count;
                else
                    initiative.Rate = rate;

                db.SaveChanges();
            }
            catch { }
            
        }
    }

    public static class SettingsManager
    {
        private static string GetValue(string name)
        {
            PInitiativesEntities db = new PInitiativesEntities();
            return db.Settings.Single(s => s.Name == name).Value;
        }

        public static string GetSettingValue(string name)
        {
            string value = GetValue(name);
            if (!String.IsNullOrEmpty(value))
                return value;
            else
                return null;
        }

        public static bool GetSettingValueBoolean(string name, bool defaultValue)
        {
            string value = GetValue(name);
            if (!String.IsNullOrEmpty(value))
                return Boolean.Parse(value);
            else
                return defaultValue;
        }

        public static int GetSettingValueInteger(string name, int defaulValue)
        {
            string value = GetValue(name);
            if (!String.IsNullOrEmpty(value))
                return int.Parse(value);
            else
                return defaulValue;
        }

        public static void AddSetting(string name, string value, string description)
        {
            PInitiativesEntities db = new PInitiativesEntities();
            Setting newSetting = new Setting(name, value, description);
            newSetting.Value = value;
            db.SaveChanges();
        }
    }

    public static class StatusHelper
    {

        public static string getStatusEN(string status)
        {
            if (status.Equals("Aktywna"))
                return "Active";
            else if (status.Equals("Zakończona"))
                return "Finished";
            else if (status.Equals("Anulowana"))
                return "Canceled";
            else
                return status;
        }

        public static string getStatusPL(string status)
        {
            if (status.Equals("Active"))
                return "Aktywna";
            else if (status.Equals("Finished"))
                return "Zakończona";
            else if (status.Equals("Canceled"))
                return "Anulowana";
            else
                return status;
        }

        public static List<String> getStatusesPL()
        {
            PInitiativesEntities _db = new PInitiativesEntities();
            List<string> listEN = _db.Dictionaries.Where(d => d.DictionaryName.Equals("InitiativeStatus")).Select(d => d.DictionaryValue).ToList<String>();
            List<string> listPL = new List<string>();
            foreach(string status in listEN){
                listPL.Add(StatusHelper.getStatusPL(status));
            }
            return listPL;
        }

    
    }

    
}